package com.chenjim.architecture.screen.home

import android.os.SystemClock
import com.chenjim.architecture.base.BaseViewModel
import com.chenjim.architecture.base.IIntent
import com.chenjim.architecture.base.IState
import com.chenjim.architecture.screen.home.HomeScreenVM.UiIntent
import com.chenjim.architecture.screen.home.HomeScreenVM.UiState
import com.chenjim.utils.Log
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject

@HiltViewModel
class HomeScreenVM @Inject constructor() : BaseViewModel<UiState, UiIntent>(UiState()) {

    override suspend fun handleIntent(intent: UiIntent) {
        when (intent) {

            UiIntent.InitIntent -> {
                Log.d("UiIntent.InitIntent")
                val logged = dateStore.isLoggedIn()
                Log.d("UiIntent.InitIntent,isLoggedIn:$logged")
                SystemClock.sleep(2000)
                updateUiState { it.copy(logged = if (logged) 1 else 0) }
            }

            UiIntent.LoginIntent -> {

            }

        }
    }

    data class UiState(
        /**
         * 1 login ;  0  not login ; -1 init
         */
        val logged: Int = -1
    ) : IState

    sealed class UiIntent : IIntent {
        data object InitIntent : UiIntent()
        data object LoginIntent : UiIntent()
    }
}

